<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
			<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<script src="https://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
		
		<script type="text/javascript">
			document.write('<script src="../../js/fix.js?rd=?rd=' + Math.random() + '"><\/script>');
		</script>
			<script type="text/javascript">
			script();
			// link(['../../js/app/common.js']);
			// link(['../../css/app.css']);
			// link(['../../css/mui.min.css',]);
			link();
		</script>
		<style type="text/css">
			   html{height:100%;}  
        body{height:100%;margin:0px;padding:0px;}  
		#r-result{
			
			height: 60%;
		}
		.mui-content{
			height: calc(100%);
		}
		.l-map_box{
			height: 40% !important;
			position: relative;
		}
		#l-map{
			height: 100% !important;
		}
		#suggestId{
			background-color: rgba(255, 255, 255, 0.9);
			}
		.mui-search{
    position: absolute;
    top: 0.1rem;
    width: 90%;
    z-index: 10;
    left: 5%;
		}
		#r-result>ul{
			box-shadow: 0px -0.02rem 0.11rem 0px #cfcfcf;
			height: 100%;
			overflow: scroll;
		}
		#r-result>ul:after{
			opacity: 0;
		}
	 .anchorBL{
		 display: none;
		 }
		 .BMap_geolocationAddress{
			 display: none !important;
		 }
		 #map_pin{
				position: absolute;
				z-index: 1200;
				text-size-adjust: none;
				 top: 50%;
				 left: 50%;
				 transform: translate(-50%,-50%);
		 }
		 .sss{
			display: none;
		 }
		</style>
	</head>

	<body>
		<div class="mui-content" >
      <div class="mui-input-row mui-search">
            <input id="suggestId" type="search"  onkeyup="enterSearch(event)" class="mui-input-clear" placeholder="搜索地址">
        </div>
        <!--百度地图显示框-->
		<div class="l-map_box">
        <div id="l-map" >
			
		</div>
		   <div id="map_pin">        <!--地图中心的图钉-->
		    <a><span style="font-size: 0.5rem; color:#47BAFE; font-weight: bold;" class="mui-icon mui-icon-plusempty"></span></a>
		</div>
		</div>
        <!--地址列表-->
        <div id="r-result"></div>
     
    </div>


		<script type="text/javascript" src="http://api.map.baidu.com/api?v=3.0&ak=cLre72ecbDV4eHGf4qx71XIAcGB4YrWy"></script>
		<script type="text/javascript">
			mui.init();
			 // 搜索事件,获取搜索关键词
			var originalHeight = document.documentElement.clientHeight || document.body.clientHeight;
				window.onresize = function() {

					//软键盘弹起与隐藏  都会引起窗口的高度发生变化
					var resizeHeight = document.documentElement.clientHeight || document.body.clientHeight;

					if(resizeHeight * 1 < originalHeight * 1) { //resizeHeight<originalHeight证明窗口被挤压了
					  document.getElementsByTagName("body")[0].style.height = originalHeight+'px';

					}
				}
					  var map = new BMap.Map("l-map");            // 创建Map实例
				 // map.centerAndZoom(basePoint,12);
				mui.plusReady(function () {

					localStorage.removeItem("jwd");
					  plus.geolocation.getCurrentPosition(function(p){
						if(p.coords.longitude=="5e-324" && p.coords.latitude=="5e-324"){
							var basePoint = new BMap.Point(116.331398,39.897445);
							 map.centerAndZoom(basePoint,14);
						}else{
							var basePoint = new BMap.Point(p.coords.longitude,p.coords.latitude);
							map.centerAndZoom(basePoint,14);//设置中心位置
						}
						 
				}, function(e){
					mui.toast("获取当前位置失败！");
				},{provider:'baidu'});
            	plus.nativeUI.showWaiting("正在定位...");
				    var context = plus.android.importClass("android.content.Context"); 
				var locationManager=plus.android.importClass("android.location.LocationManager"); 
				   var main=plus.android.runtimeMainActivity(); 
				var mainSvr=main.getSystemService(context.LOCATION_SERVICE); 
				let androidIsOpen=mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER); 
				 if(androidIsOpen){
					 	var geolocation = new BMap.Geolocation();
					 		// 开启SDK辅助定位
					 		geolocation.enableSDKLocation();
					 		geolocation.getCurrentPosition(function(r){
					 			// alert(JSON.stringify(r));
					 			if(this.getStatus() == BMAP_STATUS_SUCCESS){
					 				var mk = new BMap.Marker(r.point);
					 				map.addOverlay(mk);
									plus.nativeUI.closeWaiting();
					 				// map.panTo(r.point);
									 var basePoint = new BMap.Point(r.point.lng,r.point.lat);
									 console.log(r.address.city+r.address.district+r.address.province+r.address.street+r.address.street_number);

									map.centerAndZoom(basePoint,17);//设置中心位置
										lng=r.point.lng;
										lat=r.point.lat;
								// alert(JSON.stringify(r.point));
					 				// alert('您的位置：'+r.point.lng+','+r.point.lat);
					 			}
					 			else {
					 				// alert('failed'+this.getStatus());
					 			}        
					 		});
						
				 }else{
					 mui.toast("获取当前位置失败！可能没有打开GPS");
					 plus.nativeUI.closeWaiting();

				 }
					showCurrentPoi();
								
			 })
  
			 
			 
			        function enterSearch(event){
			    if(event.keyCode == 13) {//用户点击回车的事件号为13
			       let searchtext= document.getElementById("suggestId").value;
			       	var local = new BMap.LocalSearch(map, {      
			       	renderOptions:{map: map}      
			       });      
				    local.setSearchCompleteCallback(function(results){
						// alert(JSON.stringify(results));
						 var Point = new BMap.Point(results.Qq[0].point.lng,results.Qq[0].point.lng);
						map.centerAndZoom(Point,50);
						jQuery(".BMap_mask").next().addClass("sss");
					})
			
			       local.search(searchtext); 
				   document.activeElement.blur();

			    }
			}
			
			var opts = {anchor:BMAP_ANCHOR_BOTTOM_RIGHT,offset: new BMap.Size(10, 10)}//修改定位图标位置
			 map.addControl(new BMap.GeolocationControl(opts));    
			  // map.addControl(new BMap.OverviewMapControl(opts));    

		
			 map.addEventListener("moveend",showCurrentPoi);//设置地图移动监听
	
	
 


 map.addEventListener("moveend",showCurrentPoi);//设置地图移动监听
 
 function showCurrentPoi()//移动的时候追加附近地址名称
    {
	     
		// console.log(lnglat.split(',')[1]);
        var center = map.getCenter();
        var mOption = {
            poiRadius : 500,           //半径为1000米内的POI,默认100米
            numPois : 12                //列举出50个POI,默认10个
        }
        var myGeo = new BMap.Geocoder();
        myGeo.getLocation(center, function mCallback(rs){
                var allPois = rs.surroundingPois;       //获取全部POI（该点半径为100米内有6个POI点）
				// alert(JSON.stringify(allPois));
				var content = "";
			
                content = " <ul  class='mui-table-view mui-table-view-radio'> ";
                for(i=0;i<allPois.length;++i){
                    content = content +
                    "<li class='mui-table-view-cell'>"+
                    "<a class='mui-navigate-right' onclick='addressOk(this)'>"+
                    "<div id='icon-location' class='mui-media-object mui-pull-left'><span style='color:#47BAFE' class='mui-icon mui-icon-location-filled'></span></div>"+
                    "<div class='mui-media-body'><span class='points_title'>"+allPois[i].title+ "</span><p class='mui-ellipsis points_address'>" + allPois[i].address + "</p>"+
                    "<p class='points_other_details display_none' city_name="+allPois[i].city+" lng="+allPois[i].point.lng+" lat="+allPois[i].point.lat+"></p>"+
                    "</div></a></li>";
                       
                }
                content = content + " </ul>";
                document.getElementById("r-result").innerHTML = content;
				// 	var sss=$(".mui-selected").find(".points_title");
				// alert(sss);
				$('#suggestId').blur();
				document.activeElement.blur();
			
            },mOption);
    }
	function addressOk(obj){
		let addressName=$(obj).find(".points_title").html();
		 programmePage = plus.webview.getWebviewById('programme');//获取智能笔记页面
		mui.fire(programmePage, 'insertAddress', {  //自定义事件
			addressName:addressName
		});  
		setTimeout(function(){
			mui.back();

		},300);
	}

		</script>
	</body>

</html>
